﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKRoomInspection.Model;
using YJKRoomInspection.Utiltiy;
using YJKRevitKernel.FormUtility.ModelessFormControlSuite.ModelLayer;
using YJKInspectModelFunc;

namespace YJKRoomInspection.ShowDataForm.ModellessFrame
{
	internal class DrawLineStack : IOperateStack, IAdapatResultGet
	{
		internal DrawLineStack(ManualAddFrontRoomForm_ModellessVersion inputForm)
		{
			this.m_useForm = inputForm;
		}

		public List<AdapteRoomResulePacker> LstAdaptResult
		{
			get
			{
				return this.m_lstAdaptResult;
			}
			private set
			{
				this.m_lstAdaptResult = value;
			}
		}

		public void AddToStack(object input)
		{
			if (input is List<Curve>)
			{
				this.m_lstCurve.AddRange(input as List<Curve>);
			}
		}

		public void ClearStack()
		{
			this.m_lstCurve = new List<Curve>();
			this.m_lstFilledRegion = new List<FilledRegion>();
			this.LstAdaptResult = new List<AdapteRoomResulePacker>();
		}

		public bool TryFinishStack()
		{
			Level genLevel = this.m_useForm.UiDoc.ActiveView.GenLevel;
			Document document = this.m_useForm.UiDoc.Document;
			new List<List<List<Curve>>>();
			FilledRegionType filledRegionType = null;
			List<List<CurveLoop>> list = new List<List<CurveLoop>>();
			List<List<List<Curve>>> list2 = GpcPolygonContrllor.CurvesAggregate(CurveLoopOper.GetBoundaryCurves(document, this.m_lstCurve), 0);
			int num = 0;
			foreach (List<List<Curve>> list3 in list2)
			{
				list.Add(new List<CurveLoop>());
				foreach (List<Curve> list4 in list3)
				{
					if (list4 != null && list4.Count != 0)
					{
						CurveLoop curveLoop = CurveLoop.Create(list4);
						if (!curveLoop.IsOpen())
						{
							list[num].Add(curveLoop);
						}
					}
				}
				num++;
			}
			foreach (FilledRegionType filledRegionType2 in new FilteredElementCollector(document).OfClass(typeof(FilledRegionType)).ToElements().ToList<Element>().Cast<FilledRegionType>().ToList<FilledRegionType>())
			{
				if (filledRegionType2 != null)
				{
					filledRegionType = filledRegionType2;
					break;
				}
			}
			Transaction transaction = new Transaction(document, "MakeFilledRegion");
			try
			{
				transaction.Start();
				foreach (List<CurveLoop> list5 in list)
				{
					try
					{
						this.m_lstFilledRegion.Add(FilledRegion.Create(document, filledRegionType.Id, this.m_useForm.UiDoc.ActiveView.Id, list5));
					}
					catch (Exception)
					{
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
			List<ElementId> list6 = new List<ElementId>();
			foreach (FilledRegion filledRegion in this.m_lstFilledRegion)
			{
				this.LstAdaptResult.Add(new AdapteRoomResulePacker
				{
					AddFilledRegion = filledRegion,
					UseLevel = genLevel,
					AddRoomType = this.m_useForm.GetNowRoomTypeString(),
					RegionZVal = genLevel.Elevation
				});
				list6.Add(filledRegion.Id);
			}
			transaction = new Transaction(document, "HideFilledRegion");
			try
			{
				transaction.Start();
				this.m_useForm.UiDoc.ActiveView.HideElements(list6);
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
			return true;
		}

		private ManualAddFrontRoomForm_ModellessVersion m_useForm;

		private List<Curve> m_lstCurve = new List<Curve>();

		private List<FilledRegion> m_lstFilledRegion = new List<FilledRegion>();

		private List<AdapteRoomResulePacker> m_lstAdaptResult = new List<AdapteRoomResulePacker>();
	}
}
